package net.fdukedom.epicurus.pm.client.panels;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.text.DateFormat;
import java.util.ResourceBundle;

import javax.swing.AbstractAction;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;

import net.fdukedom.epicurus.client.ClientSession;
import net.fdukedom.epicurus.client.ui.GUIDispatcher;
import net.fdukedom.epicurus.domain.entity.User;
import net.fdukedom.epicurus.pm.client.PMClientPlugin;
import net.fdukedom.epicurus.pm.domain.entity.PrivateMessage;
import net.fdukedom.epicurus.pm.domain.util.PrivateMessageHelper;
import net.fdukedom.epicurus.resource.ResourceDispatcher;

public class ViewMessagePanel extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public ViewMessagePanel(int msgID) {
		init(msgID);
	}

	private void init(int msgID) {
		ResourceBundle bundle = ResourceDispatcher.getInstance().getBundle(
				"bundles.pm.ViewMessage");
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

		User user = ClientSession.getInstance().getUser();
		PrivateMessage msg = PrivateMessageHelper.getInstance()
				.findByIdandUser(msgID, user);

		add(new JLabel(bundle.getString("pm.viewMessage.title") + ": "
				+ msg.getTitle()));
		add(new JLabel(bundle.getString("pm.viewMessage.from") + ": "
				+ msg.getFrom().getName()));
		add(new JLabel(bundle.getString("pm.viewMessage.date") + ": "
				+ DateFormat.getDateTimeInstance().format(msg.getCreated())));

		JTextArea textArea = new JTextArea();
		textArea.setText(msg.getBody());
		textArea.setEditable(false);
		add(textArea);

		JButton button = new JButton(new AbstractAction() {
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			public void actionPerformed(ActionEvent arg0) {
				GUIDispatcher.getInstance().showPage(PMClientPlugin.PLUGINNAME,
						PMClientPlugin.INBOX_PAGE, null);
			}
		});
		button.setText(bundle.getString("pm.viewMessage.inbox"));
		
		add(button);
	}
}
